<?php

/**
 * Class containing PEGI tags related to the product
 *
 * @author Daniel Cisek
 */
class PEGI {

    /**
     * Array of tags for the product
     * @var Array
     */
    private $pegiTags;

    /**
     * Default constructor, that loads data from database
     * @param Product $product 
     */
    public function __construct($product) {
        $this->pegiTags = array();
        $this->getFromDatabase($product);
    }

    /**
     * Loads data from database for the product
     * @param type $product  
     */
    private function getFromDatabase($product) {
        $dbConnection = new DBConnection();
        $query = "SELECT gpt.tagID, tagName, tagURL FROM gamespegitags gpt
                 JOIN pegitags pt ON pt.tagID = gpt.tagID 
                 WHERE id = ? ORDER BY gpt.tagID";
        $data = array($product->id);
        $result = $dbConnection->executeQuery($query, $data);
        foreach ($result as $tag)
            $this->addTag($tag);
    }

    /**
     * Adds a tag to the array
     * @param Array $tag 
     */
    public function addTag($tag) {
        $this->pegiTags[] = $tag;
    }

    /**
     * Displays tag for product
     * @return string
     */
    public function displayTagsForProduct() {
        $html = '';
        foreach ($this->pegiTags as $tag) {
            $html .= "<img src='{$tag['tagURL']}' width='60' height='60' alt='{$tag['tagName']}' />";
        }
        return $html;
    }

    /**
     * Lists all existing tags
     * @return Array
     */
    public static function listTags() {
        $dbConnection = new DBConnection();
        $query = 'SELECT * FROM pegitags ORDER BY tagName ASC';
        return $dbConnection->executeQuery($query);
    }

    public function __toString() {
        
    }

}

?>
